package edu.hawaii.halealohacli.command;

import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertTrue;
import java.util.Calendar;
import java.util.Locale;
import org.junit.Test;
import org.wattdepot.client.WattDepotClient;

/**
 * A JUnit test for testing the DailyEnergy for correctness.
 * 
 * @author Daniel Ly
 * @author Chris Fang
 */
public class TestEnergySince {

  EnergySince testES;
  String error;

  /**
   * Create an EnergySince to test.
   */
  public TestEnergySince() {
    testES = new EnergySince();
  }

  /**
   * Test initializeCommand method for correct argument handling.
   */
  @Test
  public void testInitializeCommand() {
    // URL is blank to avoid accessing server unnecessarily
    String url = "http://";
    String[] args = new String[3];
    error = "testInitializeCommand failed: Expected true";
    assertTrue(error, testES.initializeCommand(new WattDepotClient(url), args));
    // Initialization should fail without enough arguments
    args = new String[1];
    error = "testInitializeCommand failed: Expected false";
    assertFalse(error, testES.initializeCommand(new WattDepotClient(url), args));
    // Initialization should fail with too many arguments
    args = new String[4];
    error = "testInitializeCommand failed: Expected false";
    assertFalse(error, testES.initializeCommand(new WattDepotClient(url), args));
  }

  /**
   * Test getName method for correct output.
   */
  @Test
  public void testGetName() {
    String url = "http://server.wattdepot.org:8190/wattdepot/";
    String[] args = new String[3];
    testES.initializeCommand(new WattDepotClient(url), args);
    error = "testGetName failed: Message does not match expected values";
    assertTrue(error, testES.getName().equals("energy-since"));
  }

  /**
   * Test getStartTime for successful date conversion.
   */
  @Test
  public void testGetStartTime() {
    // Future date
    error = "testGetStartTime failed: Expected null";
    assertNull(error, testES.getStartTime("2015-11-15"));
  }

  /**
   * Test convertDate for successful date conversion.
   */
  @Test
  public void testConvertDate() {
    // Incorrectly formatted date
    error = "testConvertDate failed: Expected null";
    assertNull(error, testES.convertDate("2011/11/15"));
    // Invalid date
    error = "testConvertDate failed: Expected null";
    assertNull(error, testES.convertDate("2011-13-15"));
    // Valid date
    error = "testConvertDate failed: Expected not null";
    assertNotNull(error, testES.convertDate("2011-11-15"));
  }

  /**
   * Test initializeData for successful data initialization.
   */
  @Test
  public void testInitializeData() {
    String url = "http://server.wattdepot.org:8190/wattdepot/";
    String[] args = new String[3];
    assertTrue(error, testES.initializeCommand(new WattDepotClient(url), args));
    // Correct input
    error = "testInitializeData failed: Expected true";
    assertTrue(error, testES.initializeData("Ilima-A", "2011-11-25"));
    // Unknown tower or lounge
    error = "testInitializeData failed: Expected false";
    assertFalse(error, testES.initializeData("Cosmo-P", "2011-11-25"));
    // Bad date range
    error = "testInitializeData failed: Expected false";
    assertFalse(error, testES.initializeData("Ilima-A", "2000-11-25"));
  }

  /**
   * Test toXMLGregorianCalendar for successful conversion.
   */
  @Test
  public void testToXMLGregorianCalendar() {
    Calendar date = Calendar.getInstance(Locale.US);
    error = "testToXMLGregorianCalendar failed: Expected true";
    assertNotNull(error, testES.toXMLGregorianCalendar(date));
  }
}
